package basic.concurrent;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;

/**
 * USER: lintc 【lintiancong@zhuojianchina.com】
 * DATE: 2017/5/26 9:47
 */
public class ThreadTest {
    public static void main(String[] args) {
        FutureTest();
    }
    /**
     * Thread 线程实现类
     */

    /**
     * Runnable 接口
     */

    /**
     * Callable 接口
     * call 方法相当于 run()方法  可以有返回类型 可以抛异常
     */
    public static void callableTest() {
        Callable<Integer> callable = () -> {
            Thread.currentThread().sleep(1000);
            System.out.println(1111);
            return 123123;
        };
        List<String> list = new ArrayList<>();
        list.forEach(s -> {
            System.out.println(6666);
            System.out.println(11111);
        });
        FutureTask<Integer> future = new FutureTask<>(callable);
        new Thread(future).start();
        try {
            System.out.println(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    /**
     * Future
     */
    public static void FutureTest() {
        ExecutorService service = Executors.newSingleThreadExecutor();
        Future<Integer> future = service.submit(() -> {
            Thread.currentThread().sleep(1000);
            System.out.println(1111);
            return 123123;
        });
        try {
            System.out.println(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        service.shutdown();
    }
}
